﻿using TrueSync;

namespace HEFramework
{
    public static class SkillResourceComponentSystem
    {
        [EntitySystem]
        public class SkillResourceComponentAwakeSystem : AwakeSystem<SkillResourceComponent>
        {
            protected override void Awake(SkillResourceComponent _self)
            {
            }
        }

        public static int Ready(this SkillResourceComponent _self, int _id, bool _isActive)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return (int)eSkillErrorCode.System;
            }

            var sc = _self.GetParent<SkillComponent>();
            if (sc == null)
            {
                return (int)eSkillErrorCode.System;
            }

            var ac = sc.GetParentChild<AttributeComponent>();
            if (ac == null)
            {
                return (int)eSkillErrorCode.System;
            }

            if (!sc.SkillIDs.Contains(_id))
            {
                return (int)eSkillErrorCode.System;
            }

            //消耗检测
            var skillData = dc.LoadDataTable<DRSkill>().GetData(_id);

            int[] types = _isActive ? skillData.ACostTypes : skillData.PCostTypes;
            int[] values = _isActive ? skillData.ACostValues : skillData.PCostValues;

            FP need = 0;
            for (int i = 0; i < types.Length; i++)
            {
                if (types[i] == -1)
                {
                    continue;
                }

                switch (types[i])
                {
                    case (int)eAttribute.HP:
                        if (ac.HP < values[i])
                        {
                            return (int)eSkillErrorCode.Lack_HP;
                        }

                        break;
                    case (int)eAttribute.MP:
                        if (ac.MP < values[i])
                        {
                            return (int)eSkillErrorCode.Lack_MP;
                        }

                        break;
                    case (int)eAttribute.HP_PCT:

                        need = ac.GetValue(types[i], values[i]);
                        if (ac.HP < need)
                        {
                            return (int)eSkillErrorCode.Lack_HP;
                        }

                        break;
                    case (int)eAttribute.MP_PCT:

                        need = ac.GetValue(types[i], values[i]);
                        if (ac.MP < need)
                        {
                            return (int)eSkillErrorCode.Lack_MP;
                        }


                        break;
                    case (int)eAttribute.X_HP_PCT:

                        need = ac.GetValue(types[i], values[i]);
                        if (ac.HP < need)
                        {
                            return (int)eSkillErrorCode.Lack_HP;
                        }


                        break;
                    case (int)eAttribute.X_MP_PCT:

                        need = ac.GetValue(types[i], values[i]);
                        if (ac.MP < need)
                        {
                            return (int)eSkillErrorCode.Lack_MP;
                        }


                        break;
                }
            }

            return (int)eSkillErrorCode.None;
        }

        public static void Consume(this SkillResourceComponent _self, int _id, bool _isActive)
        {
            //消耗资源
            var ac = _self.Parent.GetParentChild<AttributeComponent>();
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (ac == null || dc == null)
            {
                return;
            }

            var skillData = dc.LoadDataTable<DRSkill>().GetData(_id);
            int[] types = _isActive ? skillData.ACostTypes : skillData.PCostTypes;
            int[] values = _isActive ? skillData.ACostValues : skillData.PCostValues;
            FP need = 0;
            for (int i = 0; i < types.Length; i++)
            {
                if (types[i] == -1)
                {
                    continue;
                }

                switch (types[i])
                {
                    case (int)eAttribute.HP:
                        ac.OnHM(-values[i], 0);
                        break;
                    case (int)eAttribute.MP:
                        ac.OnHM(0, -values[i]);
                        break;
                    case (int)eAttribute.HP_PCT:
                        need = ac.GetValue(types[i], values[i]);
                        ac.OnHM(-need, 0);
                        break;
                    case (int)eAttribute.MP_PCT:
                        need = ac.GetValue(types[i], values[i]);
                        ac.OnHM(0, -need);
                        break;
                    case (int)eAttribute.X_HP_PCT:
                        need = ac.GetValue(types[i], values[i]);
                        ac.OnHM(-need, 0);
                        break;
                    case (int)eAttribute.X_MP_PCT:
                        need = ac.GetValue(types[i], values[i]);
                        ac.OnHM(0, -need);
                        break;
                }
            }
        }

        public static FP Skill_Cost(this SkillResourceComponent _self, int _id, bool _isActive, int _type)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return 0;
            }

            //消耗检测
            var skillData = dc.LoadDataTable<DRSkill>().GetData(_id);
            int[] types = _isActive ? skillData.ACostTypes : skillData.PCostTypes;
            int[] values = _isActive ? skillData.ACostValues : skillData.PCostValues;
            for (int i = 0; i < types.Length; i++)
            {
                if (_type == types[i])
                {
                    //TODO 消耗减少处理
                    return values[i];
                }
            }

            return 0;
        }
    }
}